// pages/communication/communication.js

import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    lists: [],
    isShowDelete: false,
    noData:false,
    stopLoadMoreTiem:false,
    gun:true,
    listHeight:0,
    deleteId: "",
    source: "",
    sourceType: "",
    current_page:0,
    page_size:10,
    page_start:1,
    pagination:{      //这里还是要考虑分页，只是时间问题，这里暂时不做上滑加载
      page_start: 0,
      page_size: 5
    },
    flag_del: true,      //删除标识
  },

  /**
   * 跳转添加页面
   */
  toAdd: function (e) {
    let contact = e.currentTarget.dataset.contact || {};
    console.log('contact', contact);
    wx.navigateTo({
      url: '/pages/addContact/addContact?communicate_name=' + (contact.communicate_name || "") 
        + "&communicate_mobile=" + (contact.communicate_mobile || "") 
        + "&communicate_id=" + (contact.communicate_id || "")
        + "&communicate_address=" + (contact.communicate_address || "")
        + "&type=" + this.data.sourceType,
    })
  },

  lower: function () {
    var that = this;
    if (that.stopLoadMoreTiem) {
      return;
    } 
    this.setData({
      page_start: this.data.page_start + 1 //上拉到底时将page+1后再调取列表接口
    });
     that.getCommunicateList();
  },
  /**
   * 跳转到寄件界面
   */
  toMail: function (e) {
    if(this.data.source === "index"){
      return;
    }
    let pages = getCurrentPages();

    let that = pages[pages.length - 2];
    let delta = 1;
    console.log('pages', pages)
    console.log('that', that);
    if (that.changeSenderInfo == undefined){
      that = pages[pages.length - 4];
      delta = 3;
    }    
    if(that.changeSenderInfo && typeof that.changeSenderInfo == 'function'){
      that.changeSenderInfo({
        communicate_id: e.currentTarget.dataset.contact.communicate_id,
        name: e.currentTarget.dataset.contact.communicate_name,
        phone: e.currentTarget.dataset.contact.communicate_mobile,
        address: e.currentTarget.dataset.contact.communicate_address,
        type: this.data.sourceType
      });
      wx.navigateBack({
        delta: delta,
        complete: (res) => {},
      })
    }
  },

  /**
   * 展示删除面板
   */
  showDeletePanel: function (e) {
    let that = this;
    if (!that.data.flag_del)    return;
    Dialog.confirm({
      title: '确认删除',
      message: '确定删除该通讯录联系人？',
      asyncClose: true,
      className: 'btnWidth'
    }).then(() => {
      that.setData({
        flag_del: false
      });
      wx.$api.deleteContact({ communicate_id: e.currentTarget.dataset.communicate_id}).then(res=>{
        Dialog.close();
        console.log('ressss', res.data);
        if(parseInt(res.data.code) != 200){
          wx.$Toast('fasfasfas');
          return  ;
        }
        that.getCommunicateList();
        that.setData({
          flag_del: true
        });
      })
    }).catch(() => {
      Dialog.close();
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      source: options.source,
      sourceType: options.type,
    })
    this.getCommunicateList();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // wx.computeHeight('.contact-list', (height) => {
    //   this.setData({
    //     listHeight: height
    //   })
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    alert(1)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取通讯录列表
   */
  getCommunicateList(){
    // var that = this;
    wx.$api.getCommunicateList({
      pagination:{
        "page_start":this.data.page_start,
        "page_size": 10
      }
      }).then(res => {
        if(res.data.code != 200 && res.data.code != 501){
          wx.$Toast(res.data.msg);
          return  ;
        }
      wx.hideLoading();
        const rowt = res.data.data.list
        if (rowt == null || rowt.length === 0) {
        this.setData({
          noData: true,
        })
        return;
      }   
        if (this.data.page_start === 1) {
          this.setData({
             lists: res.data.data.list
        });  
        } else {
          this.setData({
            lists: this.data.lists.concat(rowt)
          });         
        }
    });
  }
})